﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using App1AppHandler;

namespace Reborn_WorldServer.TransferCharacters
{
    public class TransferCharacterRequestWS
    {
        public Character character;

        public int MapID;
        public int Instance;
        public int MapX;
        public int MapY;
        public string PortalSound;
        public int PortalDistance = 0;
        public bool isPush = false;

        public List<string> MoveList = null;

        private Timer requestTimeout = new Timer(10000);

        //Method to run if there is a failure, default is to send a message and a ding, some circumstances we need to do something else.
        //Such as a ressurection, if the res fails because the map to transfer to is down, we need to ressurect somewhere else
        public Action<string> customErrorHandler = null;
        public Action preTransferMethod = null;

        public TransferCharacterRequestWS()
        {
            requestTimeout.AutoReset = false;
            requestTimeout.Elapsed += new ElapsedEventHandler(requestTimeout_Elapsed);
            requestTimeout.Start();
        }

        private void requestTimeout_Elapsed(object sender, ElapsedEventArgs e)
        {
            Managers.transferCharacterManager.TransferCharacterTimeout(this);   
        }

        public void killTimeout()
        {
            try
            {
                requestTimeout.Stop();
                requestTimeout.Dispose();
            }
            catch { }
        }

        public void processResult(string error, string uid, int port)
        {
            killTimeout();

            //Handle Error
            if (error != "")
            {
                //Custom handler, used for things like ressurection where we have to do something else if the transfer fails
                if (customErrorHandler != null)
                {
                    customErrorHandler.Invoke(error);
                }
                //Default behavior, send portal failure + Ding.
                else
                {
                    character.PlaySound("Ding", character.MapX, character.MapY);

                    string replyMsg = "<useportal_result>" + error + "</useportal_result>";

                    try
                    {
                        character.ClientSocket.socket.BeginSend(replyMsg);
                    }
                    catch { }
                }
                return;
            }

            if (preTransferMethod != null)
                preTransferMethod.Invoke();

            if (MoveList != null)
            {
                Managers.GameHandler.ChangePosition(character, MoveList, -1, true, isPush);
                character.ExitGame(true, PortalSound, true);
            }
            else
                character.ExitGame(true, PortalSound);

            //Send an Enter Game Result message to client so it will change servers
            string xmlMessage = "<entergameresult port=\"" + port.ToString() + "\" uid=\"" + uid + "\" />";

            try
            {
                character.ClientSocket.socket.BeginSend(xmlMessage);
            }
            catch { }
            finally
            {
                character.ClientSocket.socket.Close();
            }
        }
    }
}
